package com.example.administrator.managesystem.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.example.administrator.managesystem.R;
import com.example.administrator.managesystem.adapter.GridViewAdapter;
import com.example.administrator.managesystem.bean.GetCameraBean;
import com.example.administrator.managesystem.ui.AddLandView;

import android.widget.AdapterView.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

public class CameraGridDialog extends Dialog implements AdapterView.OnItemLongClickListener {
    private Button button_cancel, button_send;//定义取消与确认按钮
    private EditText camera_series;//定义标题文字
    private TextView add_tv;
    private GridView camera_gv;
    private boolean isShowDelete = false;
    private GridViewAdapter gridViewAdapter;
    private Context context;
    private List<GetCameraBean.DataBean> camera_list;
    private AddLandView addLandView;
    private boolean isClick=false;

    //构造方法
    public CameraGridDialog(Context context, AddLandView addLandView) {

        //设置对话框样式
        super(context, R.style.mdialog);
        this.addLandView=addLandView;
        this.context=context;
        //通过LayoutInflater获取布局
        final View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_camera_grid, null);
        camera_gv=view.findViewById(R.id.camera_gv);
        add_tv=view.findViewById(R.id.add_series_tv);
        camera_series=view.findViewById(R.id.camera_series);
        button_cancel = view.findViewById(R.id.btn_cancel);    //获取取消按钮
        button_send = view.findViewById(R.id.btn_send);  //获取确认退出按钮
        setContentView(view);  //设置显示的视图

        camera_gv.setOnItemLongClickListener(this);//长按事件监听


        add_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addListener.onAddClick(view,camera_series);
            }
        });

    }
   public void addCameraList(List<GetCameraBean.DataBean> list){
        camera_list=list;
        gridViewAdapter = new GridViewAdapter(context);
       gridViewAdapter.addDatas(camera_list);
       camera_gv.setAdapter(gridViewAdapter);
 }
    //取消监听
    public void setOnCancelListener(View.OnClickListener listener) {
        button_cancel.setOnClickListener(listener);
    }
    //取消监听
    public void setOnEnterListener(View.OnClickListener listener) {
        button_send.setOnClickListener(listener);
    }


    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Log.e("msg","position:"+position);
        Log.e("msg","isShowDelete1:"+isShowDelete);
        if (isShowDelete) {
            isShowDelete = false;
            Log.e("msg","isShowDelete1:"+isShowDelete);
            camera_gv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                }
            });

        } else {
            isShowDelete = true;
            Log.e("msg","isShowDelete2:"+isShowDelete);
            gridViewAdapter.setIsShowDelete(isShowDelete);
                camera_gv.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                        Log.e("msg","final int position:"+position);
                        Log.e("msg"," isShowDelete:"+isShowDelete);
                        AlertDialog.Builder builder = new AlertDialog.Builder(context);
                        builder.setTitle( camera_list.get(position).getDevice_number());
                        builder.setMessage("确认删除吗?");
                        //点击对话框以外的区域是否让对话框消失
                        builder.setCancelable(false);
                        //设置正面按钮
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                addLandView.showDeleteMsg(camera_list.get(position).getCamera_id());
                                delete(position);//删除选中项
                                Log.i("------>", "进来了么");
                                // gridViewAdapter = new GridViewAdapter(context);//重新绑定一次adapter
                                gridViewAdapter.addDatas(camera_list);
                                //camera_gv.setAdapter(gridViewAdapter);
                                gridViewAdapter.notifyDataSetChanged();//刷新gridview
                                dialog.dismiss();

                            }
                        });
                        //设置反面按钮
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        builder.create().show();


                    }

                });

        }


        gridViewAdapter.setIsShowDelete(isShowDelete);//setIsShowDelete()方法用于传递isShowDelete值

        return true;
    }
    private void delete(int position) {//删除选中项方法
         List newList = new ArrayList<>();
        if (isShowDelete) {
            camera_list.remove(position);
            isShowDelete = false;
        }
        newList.addAll(camera_list);
        camera_list.clear();
        camera_list.addAll(newList);
    }


    private OnAddCameraClickListener addListener;
    public interface OnAddCameraClickListener {
        void onAddClick( View view,EditText editText);
    }
    public void setOnAddCameraClickListener (OnAddCameraClickListener listener) {
        this.addListener=listener;
    }

}
